package com.zxy.study;

import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import android.util.Log;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static org.junit.Assert.assertEquals;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
	private static final String TAG = "AAA";

	@Test
	public void useAppContext() {
		// Context of the app under test.
		Context appContext = InstrumentationRegistry.getInstrumentation().getContext();

		assertEquals("com.zxy.study", appContext.getPackageName());
	}

	@Test
	public void testRx() {
		//		Observable.just(1, 2, 3).map(new Function<Integer, Integer>() {
		//			@Override
		//			public Integer apply(Integer integer) throws Exception {
		//				return integer * 10;
		//			}
		//		}).subscribe(new Consumer<Integer>() {
		//			@Override
		//			public void accept(Integer integer) throws Exception {
		//				System.out.println("accept: " + integer);
		//			}
		//		});


		//		Observable.create(new ObservableOnSubscribe<Integer>() {
		//			@Override
		//			public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
		//				//				System.out.println("------------emit 1");
		//				emitter.onNext(1);
		//				//				System.out.println("------------emit 2");
		//				emitter.onNext(2);
		//			}
		//		}).flatMap(new Function<Integer, ObservableSource<?>>() {
		//			@Override
		//			public ObservableSource<?> apply(Integer integer) throws Exception {
		//				int delayTime = (int) (1 + Math.random() * 10);
		//				List<String> data = new ArrayList<>();
		//				for (int i = 0; i < 3; i++) {
		//					data.add("flatMap=" + integer);
		//				}
		//				return Observable.fromIterable(data);
		//			}
		//		}).subscribe(new Observer<Object>() {
		//			@Override
		//			public void onSubscribe(Disposable d) {
		////				Log.e(TAG, "onSubscribe: ");
		//			}
		//
		//			@Override
		//			public void onNext(Object o) {
		//				Log.e(TAG, "onNext: --" + o);
		//			}
		//
		//			@Override
		//			public void onError(Throwable e) {
		////				Log.e(TAG, "onError: ");
		//			}
		//
		//			@Override
		//			public void onComplete() {
		////				Log.e(TAG, "onComplete: ");
		//			}
		//		});

		Observable.create(new ObservableOnSubscribe<Integer>() {
			@Override
			public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception {
				e.onNext(1);
				e.onNext(2);
				e.onNext(3);
			}
		})
			.concatMap(new Function<Integer, ObservableSource<String>>() {
				@Override
				public ObservableSource<String> apply(@NonNull Integer integer) throws Exception {
					List<String> list = new ArrayList<>();
					for (int i = 0; i < 3; i++) {
						list.add("I am value " + integer);
					}
					int delayTime = (int) (1 + Math.random() * 10);
					return Observable.fromIterable(list).delay(delayTime, TimeUnit.MILLISECONDS);
				}
			})
			.subscribeOn(Schedulers.newThread())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new Consumer<String>() {
				@Override
				public void accept(@NonNull String s) throws Exception {
					Log.e(TAG, "flatMap : accept : " + s + "\n");
				}
			});

	}

}
